<?php

class CAjaxJavaScript{
	
	/**
	 * Funcion para obtener SCRIPTS del servidor
	 * Genera una llamada a la funcion callActionWithForm de JavaScript para
	 * realizar un llamado con formulario <POST>.
	 * @param Array[String] $params Parametros que se agregan a la URL
	 * @param String $command Default comando actual App::$Request->_command
	 * @param String $action  Default accion actual App::$Request->_action
	 * @param String $layout Defaul ['view']['ajax_layout']
	 * @param String $div_modal Defaul ['enviroment']['div_modal_updater']
	 * @param String $div_commander Default ['enviroment']['div_command']
	 * @param String $form Formulario de CHtml::$actual_form
	 * @param String $titl ['enviroment']['app_name']
	 */
	public function callActionWithForm
	($params=null, $command=null, $action=null, $layout=null, $div_modal=null, $div_commander=null, $form=null, $title=null){
		$command = ($command) ? $command : App::$Request->_command;
		$action = ($action) ? $action : App::$Request->_action;
		$layout = ($layout) ? $action : Config::$opt['view']['ajax_layout'];
		$div_modal = ($div_modal) ? $div_modal : Config::$opt['enviroment']['div_modal'];
		$div_commander = ($div_commander) ? $div_commander : Config::$opt['enviroment']['div_commander'];
		$form = ($form) ? $form : CHtml::$actual_form;
		$title = ($title) ? $title : Config::$opt['enviroment']['app_name'];
		
		$url = App::$Response->makeUrl($command, $action, $layout, $params);

		return "callActionWithForm(this, '$url', '$div_modal', '$div_commander', '$form', '$title');";
	}
	
	/**
	* Funcion para obtener SCRIPTS del servidor
	* Genera una llamada a la funcion callAction de JavaScript para
	* realizar un llamado sin formulario <GET>
	* @param String $command Default comando actual App::$Request->_command
	* @param String $action Default accion actual App::$Request->_action
	* @param String $layout Defaul ['view']['ajax_layout']
	* @param String $div_modal Defaul ['enviroment']['div_modal_updater']
	* @param String $div_commander Default ['enviroment']['div_command']
	*/
	public function callAction
	($command=null, $action=null, $layout=null, $div_modal=null, $div_commander=null){
		$command = ($command) ? $command : App::$Request->_command;
		$action = ($action) ? $action : App::$Request->_action;
		$layout = ($layout) ? $action : Config::$opt['view']['ajax_layout'];
		$div_modal = ($div_modal) ? $div_modal : Config::$opt['enviroment']['div_modal'];
		$div_commander = ($div_commander) ? $div_commander : Config::$opt['enviroment']['div_commander'];
	
		$url = App::$Response->makeUrl($command, $action, $layout);
	
		return "callAction('$url', '$div_modal', '$div_commander');";
	}

	/**
	 * Funcion para obtener HTML del servidor
	 * Genera una llamada a la funcion callUpdateDiv de JavaScript para
	 * realizar un llamado sin formulario <GET>
	 * @param String $command Default comando actual App::$Request->_command
	 * @param String $action $action Default accion actual App::$Request->_action
	 * @param String $layout Defaul ['view']['ajax_layout']
	 * @param Array $params Parametros que se incrustaran a la peticion
	 * @param Bool	$modal Indica si la actualizacion sera modal
	 * @param String $div_update Defaul ['enviroment']['div_body']
	 * @param String $title Defaul ['enviroment']['app_name']
	 */
	public function callUpdateDiv
	($command=null, $action=null, $layout=null, $params=null, $modal=true, $div_update=null, $title=null){
		$command = ($command) ? $command : App::$Request->_command;
		$action = ($action) ? $action : App::$Request->_action;
		$layout = ($layout) ? $action : Config::$opt['view']['ajax_layout'];
		$div_update = ($div_update) ? $div_update : Config::$opt['enviroment']['div_body'];
		$div_modal = ($div_modal) ? $div_modal : Config::$opt['enviroment']['div_modal'];
		$title = ($title) ? $title : Config::$opt['enviroment']['app_name'];
		$modal = ($modal) ? 'true' : 'false';
		
		$url = App::$Response->makeUrl($command, $action, $layout, $params);
		
		return "callUpdateDiv('$url', '$div_update', $modal, '$div_modal', '$title');";
	}
	
	/**
	* Funcion para obtener HTML del servidor
	* Genera una llamada a la funcion callUpdateDivWithForm de JavaScript para
	* realizar un llamado con formulario <POST>
	* @param String $command Default comando actual App::$Request->_command
	* @param String $action $action Default accion actual App::$Request->_action
	* @param String $layout Defaul ['view']['ajax_layout']
	* @param String $form id del formulario a enviar default CHtml::$actual_form
	* @param Array[String] $params parametros a incluir en el url de peticion.
	* @param Bool $modal Defaul false
	* @param String $div_update Defaul ['enviroment']['div_body']
	* @param String $title Defaul ['enviroment']['app_name']
	*/
	public function callUpdateDivWithForm
	($command=null, $action=null, $layout=null, $form=null, $params=null, $modal=false, $div_update=null, $title=null){
		$command = ($command) ? $command : App::$Request->_command;
		$action = ($action) ? $action : App::$Request->_action;
		$layout = ($layout) ? $action : Config::$opt['view']['ajax_layout'];
		$div_update = ($div_update) ? $div_update : Config::$opt['enviroment']['div_body'];
		$div_modal = ($div_modal) ? $div_modal : Config::$opt['enviroment']['div_modal'];
		$title = ($title) ? $title : Config::$opt['enviroment']['app_name'];
		$modal = ($modal) ? 'true' : 'false';	
		$form = ($form) ? $form : CHtml::$actual_form;

		$url = App::$Response->makeUrl($command, $action, $layout, $params);
	
		return "callUpdateDivWithForm(this, '$url', '$div_update', '$form', $modal, '$div_modal', '$title');";
	}
}